package com.liangsui.mss.controller.system;

import com.liangsui.mss.dto.DictDataDto;
import com.liangsui.mss.service.DictDataService;
import com.liangsui.mss.utils.ShiroSecurityUtils;
import com.liangsui.mss.vo.AjaxResult;
import com.liangsui.mss.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * DictDataController
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/4 17:29
 */
@RestController
@RequestMapping("system/dict/data")
public class DictDataController {

    @Autowired
    private DictDataService dictDataService;

    /**
     * 分页查询字典数据列表
     * @param dictDataDto
     * @return
     */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DictDataDto dictDataDto) {
        DataGridView dataGridView = dictDataService.listForPage(dictDataDto);
        return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
    }

    /**
     * 根据dictId查询字典数据
     * @param dictId
     * @return
     */
    @GetMapping("getOne/{dictId}")
    public AjaxResult getDictData(@PathVariable @Validated
                                  @NotNull(message = "字典数据ID不能为空") Long dictId) {
        return AjaxResult.success(dictDataService.selectDictDataById(dictId));
    }

    /**
     * 新增字典数据
     * @param dictDataDto
     * @return
     */
    @PostMapping("addDictData")
    public AjaxResult addDictData(@Validated DictDataDto dictDataDto) {
        dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(dictDataService.insert(dictDataDto));
    }

    /**
     * 修改字典数据
     * @param dictDataDto
     * @return
     */
    @PostMapping("updateDictData")
    public AjaxResult updateDictData(@Validated DictDataDto dictDataDto) {
        dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(dictDataService.update(dictDataDto));
    }

    /**
     * 删除字典数据
     * @param dictIds
     * @return
     */
    @DeleteMapping("deleteDictDataByIds/{dictIds}")
    public AjaxResult deleteDictData(@PathVariable @Validated
                                     @NotEmpty(message = "要删除的字典数据ID不能为空") Long[] dictIds) {
        return AjaxResult.toAjax(dictDataService.deleteDictData(dictIds));
    }

    /**
     * 根据字典类型查询字典数据
     */
    @GetMapping("getDataByType/{dictType}")
    public AjaxResult getDataByType(@PathVariable @Validated @NotEmpty(message = "数据字典类型不能为空") String dictType) {
        return AjaxResult.success(this.dictDataService.selectDictDataByDictType(dictType));
    }
}
